﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using VitCode.Castle.DataAccess;

namespace AMS.Web.BaseData
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        BaseDataAccess m_Acc = new BaseDataAccess();
        XmlDocument m_XmlDoc = new XmlDocument();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SelectDepartmentAsset();
            SelectDepartmentAssetValue();
        }

        private void SelectDepartmentAsset()
        {
            string xmlFilePath = AppDomain.CurrentDomain.BaseDirectory + "Report/XMLFile/AMS/AssetDistributeReport.xml";
            var ds = new DataSet();
            ds.ReadXml(xmlFilePath);
            var xmlObj = new XmlDocument();
            xmlObj.Load(xmlFilePath);

            XmlNode xmlTotalOjb = xmlObj.SelectSingleNode("chart");

            xmlTotalOjb.RemoveAll();


            var dt = m_Acc.GetDataTableBySql("select * from v_Department_Asset_Distribute");
            if (dt != null && dt.Rows.Count > 0)
            {
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    var strDepartmentName = dt.Rows[i]["Name"].ToString();
                    var strAssetCount = dt.Rows[i]["count"].ToString();
                    XmlElement el = xmlObj.CreateElement("set");
                    el.SetAttribute("label", strDepartmentName);
                    el.SetAttribute("value", strAssetCount);
                    xmlTotalOjb.AppendChild(el);
                }
            }
            xmlObj.Save(xmlFilePath);


        }
        private void SelectDepartmentAssetValue()
        {
            string xmlFilePath = AppDomain.CurrentDomain.BaseDirectory + "Report/XMLFile/AssetValueDistributeReport.xml";
            var ds = new DataSet();
            ds.ReadXml(xmlFilePath);
            var xmlObj = new XmlDocument();
            xmlObj.Load(xmlFilePath);

            XmlNode xmlTotalOjb = xmlObj.SelectSingleNode("chart");

            xmlTotalOjb.RemoveAll();


            var dt = m_Acc.GetDataTableBySql("select * from [v_Department_Asset_Value]");
            if (dt != null && dt.Rows.Count > 0)
            {
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    var strDepartmentName = dt.Rows[i]["Name"].ToString();
                    var strAssetValue = dt.Rows[i]["value"].ToString();
                    XmlElement el = xmlObj.CreateElement("set");
                    el.SetAttribute("label", strDepartmentName);
                    el.SetAttribute("value", strAssetValue);
                    xmlTotalOjb.AppendChild(el);
                }
            }
            xmlObj.Save(xmlFilePath);
        }


    }
}